package com.landian.commons.jpa.builder;

import com.google.common.collect.Lists;
import com.landian.commons.jpa.order.Order;
import com.landian.commons.jpa.utils.PathUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * 排序构建器
 * @author caojl
 * @create 2018/05/24
 **/
public class SortBuilderRef {

    public static Sort build(Order... orders) {
        ArrayList<Order> list = Lists.newArrayList(orders);
        return build(list);
    }

    public static Sort build(List<Order> list) {
        List<Sort.Order> orderList = list.stream().map(b -> {
            Sort.Direction direction = b.getDirection();
            String fieldName = PathUtils.getFieldName(b.getPath());
            return new Sort.Order(direction, fieldName);
        }).collect(toList());
        return new Sort(orderList);
    }

    public static Sort build(com.landian.commons.jpa.order.Sort sort) {
        return build(sort.getList());
    }
}
